Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

  1. 1 -> A
  2. 2 -> B
  3. 3 -> C
  4. ...
  5. 26 -> Z
  6. 27 -> AA
  7. 28 -> AB

Solution:

  1. public class Solution {
  2. public String convertToTitle(int n) {
  3. String res = "";
  4. while (n >= 1) {
  5. res = (char)('A' + (n - 1) % 26) + res;
  6. n = (n - 1) / 26;
  7. }
  8. return res;
  9. }
  10. }